﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.IO;
using System.Text;

namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JSDWFAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get { return "JSDWFList"; }
        }

        protected int showtype
        {
            get
            {
                return GetRequestValue<int>("showtype", 0);
            }
        }

        protected int FolderID
        {
            get
            {
                return GetRequestValue<int>("FolderID", 0);
            }
        }

        private JSDWFFolderInfo _folderObj = null;
        protected JSDWFFolderInfo DWFFolder
        {
            get
            {
             //   JSDWFFolderInfo _folderObj = JSDWFFolderController.GetByFolderID(FolderID);
                if (_folderObj == null)
                {
                    if (FolderID == 0)
                        _folderObj = new JSDWFFolderInfo() { FolderName = "图纸根目录", FolderPath = JSDWFFolderController.DWFUploadFolderPath };
                    else
                        _folderObj = JSDWFFolderController.GetByFolderID(FolderID);
                }

                return _folderObj;
            }
        }

        protected string GetAdtlvlID()
        {
            List<SAuditLevelInfo> list = SAuditLevelController.GetByType(SAuditLevelType.DWF);
            if (list.Count <= 0)
                return "";

            /// <ul><li><a href="JSDWF/JSDWFFolderPageList.aspx?ParFolderID=1" target="ajax" rel="jbsxBox">尿检</a></li></ul>
            StringBuilder sb = new StringBuilder();
         //   sb.Append("<ul>");
            foreach (SAuditLevelInfo itm in list)
            {
              //  sb.Append("<option>");
                sb.AppendFormat("<option value='{0}'>{1}</option>", itm.AdtlvlID, itm.Title);

                // sb.Append(GetFolderTree(itm.FolderID));

               // sb.Append("</option>");
            }
            //sb.Append("</ul>");

            return sb.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (FolderID == 0)
            {
                Response.Write("不能添加图纸到根目录,请选择一个图纸分类");
                Response.End();
                return;
            }


            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }
            if (GetRequestValue<string>("ajaxmethod", "") == "upload")
            {
              Response.Write(  upload(DWFFolder.FolderPath));
              Response.End();
              return;
            }


        }
        private void addmenu()
        {
            JSDWFInfo obj = Request.SerializableObject<JSDWFInfo>();
            // obj.AgentName = GetRequestValue<string>("dwz.orgLookup.AgentName", "");

            // obj.UserName = GetRequestValue<string>("dwz_orgLookup.UserName", "");
            //图纸上传相关
            string FolderPath = DWFFolder.FolderPath;
            string FileName =  upload(FolderPath);
            //end
            if (FileName == "")
            {
                Response.Write("文件上传失败,请联系IT部");
                return;
            }
            if (FileName == "-1")
            {
                Response.Write("文件[" + FileName + "]已存在，请确认名称是否正常，是否重复上传.");
                return;
            }
            obj.FileName = FileName;
            obj.Title = FileName;
            int r = JSDWFController.Add(obj);

            AddLog(Permission.新增, obj);

            if (r > 0)
                Response.Write("<script>top.alertMsg.correct('添加成功')</script>");
            else
            {
                Response.Write("添加失败");
            }
        }

        private string upload(string folderpath)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            /// '状态信息
            System.Text.StringBuilder strMsg = new System.Text.StringBuilder("");
            strMsg.Append("上传的文件分别是：<hr color='red'/>");
            try
            {
                string fileName = "", fileExtension = "";
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    ///'检查文件扩展名字
                    HttpPostedFile postedFile = files[iFile];                   
                    //
                    fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    //是否已存在
                    if (File.Exists(System.Web.HttpContext.Current.Request.MapPath(folderpath + "/") + fileName))
                    {
                        return "-1";
                    }
                    if (fileName != "")
                    {
                        fileExtension = System.IO.Path.GetExtension(fileName);
                        //strMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
                        //strMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
                        //strMsg.Append("上传文件的文件名：" + fileName + "<br>");
                        //strMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr>");
                        ///'可根据扩展名字的不同保存到不同的文件夹
                        ///注意：可能要修改你的文件夹的匿名写入权限。
                        
                        if(!Directory.Exists(System.Web.HttpContext.Current.Request.MapPath(folderpath)))
                            Directory.CreateDirectory(System.Web.HttpContext.Current.Request.MapPath(folderpath ));

                        postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(folderpath + "/") + fileName);
                    }
                }
                //strStatus.Text = strMsg.ToString();
                return fileName;
            }
            catch (System.Exception Ex)
            {
                //strStatus.Text = Ex.Message;
                return "";
            }
        

        }


        string jsonString(string str)
        {
            str = str.Replace("\\", "\\\\");
            str = str.Replace("/", "\\/");
            str = str.Replace("'", "\\'");
            return str;
        }


        string GetFileExt(string FullPath)
        {
            if (FullPath != "") return FullPath.Substring(FullPath.LastIndexOf('.') + 1).ToLower();
            else return "";
        }

        void CreateFolder(string FolderPath)
        {
            if (!System.IO.Directory.Exists(FolderPath)) System.IO.Directory.CreateDirectory(FolderPath);
        }
    }
}